package com.dc.refactoring._1_重新组织函数._5_IntroduceExplainingVariable;


import com.dc.refactoring._1_重新组织函数._4_ReplaceTempWithQuery.BaseClass;

/**
 * Description: 引入解释性变量
 * @author: gdc
 * @date: 2020/11/13
 * @version 1.0
 */
public class Test1 extends BaseClass {

    /**
     * 改造前
     */
    public double getPriceOld() {
        return _itemPrice * _quantity - Math.max(0, _quantity - 500) * _quantity * 0.05
                + Math.min(100.0, _quantity * _itemPrice * 0.01);
    }

    /**
     * 改造后
     */
    public double getPriceNew() {
        // 基础价格
        int basePrice = _itemPrice * _quantity;
        // 折扣
        double quantityDiscount = Math.max(0, _quantity - 500) * _quantity * 0.05;
        // 运费
        double shipping = Math.min(100.0, _quantity * _itemPrice * 0.01);

        return basePrice - quantityDiscount + shipping;
    }
}
